package com.iqiyi.pps.epg.web.webapp.action.stream;

import com.alibaba.fastjson.JSONObject;
import com.iqiyi.pps.epg.api.model.web.stream.TLiveStream;
import com.iqiyi.pps.epg.core.dao.base.Page;
import com.iqiyi.pps.epg.core.utils.ApiHttpRequestUtil;
import com.iqiyi.pps.epg.web.utils.InnerApiUtil;
import com.iqiyi.pps.epg.web.webapp.action.BaseAction;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2015/1/9.
 */
@Namespace("/stream/p2pStream")
public class P2pStreamAction extends BaseAction<TLiveStream> {

    private Page<TLiveStream> page;

    private String fatherId;
    private String streamName;
    private String streamIp;

    @Override
    public String list() throws Exception {
        return null;
    }

    @Override
    @Action(value = "input")
    public String input() throws Exception {
        return SUCCESS;
    }

    @Override
    @Action(value = "save")
    public String save() throws Exception {
        String url = String.format(InnerApiUtil.BASE_URL, InnerApiUtil.P2P_ONLINE);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("fatherId", fatherId);
        params.put("streamName", streamName);
        params.put("streamIp", streamIp);
        Map<String, Object> headers = new HashMap<String, Object>();
        headers.put("Sign", InnerApiUtil.TEST_SIGN);
        String msg = ApiHttpRequestUtil.forceGetResponseMsg(url, headers, params);
        addActionMessage(msg);
        return INPUT;
    }

    @Override
    @Action(value = "delete")
    public String delete() throws Exception {
        String url = String.format(InnerApiUtil.BASE_URL, InnerApiUtil.P2P_OFFLINE);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("fatherId", fatherId);
        params.put("streamName", streamName);
        Map<String, Object> headers = new HashMap<String, Object>();
        headers.put("Sign", InnerApiUtil.TEST_SIGN);
        JSONObject jsonObject = ApiHttpRequestUtil.postMethod(url, headers, params);
        code = jsonObject.getString("code");
        message = jsonObject.getString("msg");
        renderAjaxMessage(code, message);
        return null;
    }

    @Override
    protected void prepareModel() throws Exception {

    }

    @Override
    public TLiveStream getModel() {
        return null;
    }

    public Page<TLiveStream> getPage() {
        return page;
    }

    public void setPage(Page<TLiveStream> page) {
        this.page = page;
    }

    public String getFatherId() {
        return fatherId;
    }

    public void setFatherId(String fatherId) {
        this.fatherId = fatherId;
    }

    public String getStreamName() {
        return streamName;
    }

    public void setStreamName(String streamName) {
        this.streamName = streamName;
    }

    public String getStreamIp() {
        return streamIp;
    }

    public void setStreamIp(String streamIp) {
        this.streamIp = streamIp;
    }
}
